/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;

import com.phonebooksharing.android.activities.pref.PreferencesActivity;
import com.phonebooksharing.android.provider.PhonebookSharing.Settings;
import com.phonebooksharing.android.widgets.drawable.FastBitmapDrawable;

public class Global {

    public static final String LOG_TAG = "PBS";
    public static boolean isNew = false;
    public static String authItem;
    public static FastBitmapDrawable postCardBack, postCardFront, tempBitmap;
    public static final int PAGINATOR_PAGE_SIZE = 15;//TODO: hard coded?
    public static double unitValue;
    public static final int NOTIFICATION_GUID = 921661236;
    
//    public static final long WHITE_BORDER_BITMAP  = Long.MAX_VALUE -1, 
//                              BLUE_BORDER_BITMAP   = Long.MAX_VALUE -2, 
//                              YELLOW_BORDER_BITMAP = Long.MAX_VALUE -3;
//
//    public static final String BASE_URL = "//192.168.1.20:7001/PhonebookSharingServices";
//    public static final String BASE_URL = "//192.168.232.1:7001/PhonebookSharingServices";
//    public static final String BASE_URL = "//services.phonebooksharing.com/PhonebookSharingServices";
//    public static final String BASE_URL = "//192.168.24.1:8080/PhonebookSharingServices";
    public static final String BASE_URL = "//192.168.24.1:8080/PbsFakeServer";
    
    public static final String CONTACT_SERVLET_URL = BASE_URL + "/ContactServlet?ac=%s&p=%s&ps=%s&c=%s&a=%s";
    public static final String IMAGE_SERVLET_URL = BASE_URL + "/ImageServlet?ac=%s&q=%s&c=%s&p=%s&ps=%s&mt=%s&w=%s&h=%s";
    public static final String VIDEO_SERVLET_URL = BASE_URL + "/VideoServlet?ac=%s&q=%s&c=%s&p=%s&ps=%s&mt=%s&w=%s&h=%s";
    public static final String WISHLIST_SERVLET_URL = BASE_URL + "/WishlistServlet?ac=%s&q=%s&c=%s&p=%s&ps=%s";
    public static final String DASHBOARD_SERVLET_URL = BASE_URL + "/DashboardServlet?ac=%s&q=%s&p=%s&ps=%s";
    public static final String PROFILE_SERVLET_URL = BASE_URL + "/ProfileServlet?ac=%s&q=%s";
    public static final String MESSAGE_SERVLET_URL = BASE_URL + "/MessageServlet?ac=%s&t=%s&q=%s&mt=%s";
    public static final String LOCATION_SERVLET_URL = BASE_URL + "/LocationServlet?ac=%s";

    public static void loadConfig(Context cx) {
        Cursor c = null;
        SharedPreferences pref;
        try {
            pref = cx.getSharedPreferences(PreferencesActivity.PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
            c = cx.getContentResolver().query(Settings.CONTENT_URI, new String[]{Settings.AUTH_ID}, null, null, null);
            if(c != null && c.moveToFirst()) {
                Global.authItem = c.getString(0);
            }
            unitValue = new Double(pref.getString(PreferencesActivity.PREF_DISPLAY_UNIT_CONVERT, "1"));
        } finally {
            CloseableUtil.safeClose(c);
        }
    }
    
    public static void setTempBitmap(FastBitmapDrawable temp) {
        if(tempBitmap != null) tempBitmap.setCallback(null);
        tempBitmap = temp; //may be null, should clean up
    }
    
    public static void setPostcard(FastBitmapDrawable front, FastBitmapDrawable back) {
        if(postCardFront != null) postCardFront.setCallback(null);
        if(postCardBack != null) postCardBack.setCallback(null);
        
        if(front != null) postCardFront = front;
        if(back != null) postCardBack  = back;
        
        if(front == null && back == null) {
            postCardFront = null;
            postCardBack  = null;
        }
    }
    
    /**
     * should be called when application finishes.
     */
    public static void cleanUp() {
        Log.d(LOG_TAG, "Cleaning up all static resources...");
        authItem = null;
        setPostcard(null, null);
        GalleryUtilities.cleanupCache();
        IOUtilities.cleanUp();
        Log.d(LOG_TAG, "Finish cleaned up...");
    }
}
